<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Volatile (computer programming)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Volatile_(computer_programming)"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Volatile_computer_programming rootpage-Volatile_computer_programming skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main">volatile (computer programming)</span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <a href="Variable_(computer_science)" title="Variable (computer science)">variable</a> is said to be <i><b>volatile</b></i> if its <a href="Value_(computer_science)" title="Value (computer science)">value</a> can be read or modified asynchronously by something other than the current <a href="Thread_(computing)" title="Thread (computing)">thread of execution</a>.
The value of a <code>volatile</code> variable may spontaneously change for reasons such as:
sharing values with other threads;
sharing values with asynchronous <a href="Signal_handler" class="mw-redirect" title="Signal handler">signal handlers</a>;
accessing hardware devices via <a href="Memory-mapped_I/O" class="mw-redirect" title="Memory-mapped I/O">memory-mapped I/O</a> (where you can send and receive messages from <a href="Peripheral_device" class="mw-redirect" title="Peripheral device">peripheral devices</a> by reading from and writing to memory).
Support for these use cases varies considerably among the programming languages that have the <code>volatile</code> keyword.
Volatility can have implications regarding function <a href="Calling_convention" title="Calling convention">calling conventions</a> and how variables are stored, accessed and cached.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="In_C_and_C++">In C and C++</h2></div>
<p>In C and C++, <code>volatile</code> is a <a href="Type_qualifier" title="Type qualifier">type qualifier</a>, like <code><a href="Const_(computer_programming)" title="Const (computer programming)">const</a></code>, and is a part of a <a href="Data_type" title="Data type">type</a> (e.g. the type of a variable or field).
</p><p>The behavior of the <code>volatile</code> keyword in C and C++ is sometimes given in terms of suppressing optimizations of an <a href="Optimizing_compiler" title="Optimizing compiler">optimizing compiler</a>: 1- don't remove existing <code>volatile</code> reads and writes, 2- don't add new <code>volatile</code> reads and writes, and 3- don't reorder <code>volatile</code> reads and writes. However, this definition is only an approximation for the benefit of new learners, and this approximate definition should not be relied upon to write real production code.
</p><p>In C, and consequently C++, the <code>volatile</code> keyword was intended to:<sup id="cite_ref-auto_1-0" class="reference"><a href="#cite_note-auto-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
<ul><li>Allow access to <a href="Memory-mapped_I/O" class="mw-redirect" title="Memory-mapped I/O">memory-mapped I/O</a> devices.</li>
<li>Allow preserving values across a <code><a href="Setjmp" class="mw-redirect" title="Setjmp">longjmp</a></code>.</li>
<li>Allow sharing values between signal handlers and the rest of the program in <code>volatile</code> <code>sig_atomic_t</code> objects.</li></ul>
<p>The C and C++ standards allow writing portable code that shares values across a <code><a href="Setjmp" class="mw-redirect" title="Setjmp">longjmp</a></code> in <code>volatile</code> objects, and the standards allow writing portable code that shares values between signal handlers and the rest of the code in <code>volatile</code> <code>sig_atomic_t</code> objects. Any other use of <code>volatile</code> keyword in C and C++ is inherently non-portable or incorrect. In particular, writing code with the <code>volatile</code> keyword for <a href="Memory-mapped_I/O" class="mw-redirect" title="Memory-mapped I/O">memory-mapped I/O</a> devices is inherently non-portable and always requires deep knowledge of the specific target C/C++ implementation and platform.
</p>
<div class="mw-heading mw-heading3"><h3 id="Multi-threading">Multi-threading</h3></div>
<p>It is a common misconception that the <code>volatile</code> keyword is useful in portable <a href="Thread_(computing)" title="Thread (computing)">multi-threading</a> code in C and C++. The <code>volatile</code> keyword in C and C++ has <i>never</i> functioned as a useful, portable tool for <i>any</i> multi-threading scenario.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> Unlike the <a href="Java_(programming_language)" title="Java (programming language)">Java</a> and <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a> programming languages, operations on <code>volatile</code> variables in C and C++ are not <a href="Atomic_operation" class="mw-redirect" title="Atomic operation">atomic</a>, and operations on <code>volatile</code> variables do not have sufficient <a href="Memory_ordering" title="Memory ordering">memory ordering</a> guarantees (i.e. <a href="Memory_barrier" title="Memory barrier">memory barriers</a>). Most C and C++ compilers, linkers, and runtimes simply do not provide the necessary memory ordering guarantees to make the <code>volatile</code> keyword useful for <i>any</i> multi-threading scenario. Before the C11 and C++11 standards, programmers were forced to rely on guarantees from the individual implementations and platforms (e.g. POSIX and WIN32) to write <a href="Thread_(computing)" title="Thread (computing)">multi-threading</a> code. With the modern C11 and C++11 standards, programmers can write portable <a href="Thread_(computing)" title="Thread (computing)">multi-threading</a> code using new portable constructs such as the <code>std::atomic<T></code> templates.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Example_of_memory-mapped_I/O_in_C">Example of memory-mapped I/O in C</h3></div>
<p>In this example, the code sets the value stored in <code>foo</code> to <code>0</code>. It then starts to <a href="Polling_(computer_science)" title="Polling (computer science)">poll</a> that value repeatedly until it changes to <code>255</code>:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">bar</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">foo</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">255</span><span class="p">)</span>
<span class="w"> </span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>An <a href="Optimizing_compiler" title="Optimizing compiler">optimizing compiler</a> will notice that no other code can possibly change the value stored in <code>foo</code>, and will assume that it will remain equal to <code>0</code> at all times. The compiler will therefore replace the function body with an <a href="Infinite_loop" title="Infinite loop">infinite loop</a> similar to this:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="nf">bar_optimized</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="nb">true</span><span class="p">)</span>
<span class="w"> </span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>However, the programmer may make <code>foo</code> refer to another element of the computer system such as a <a href="Hardware_register" title="Hardware register">hardware register</a> of a device connected to the <a href="CPU" class="mw-redirect" title="CPU">CPU</a> which may change the value of <code>foo</code> while this code is running. (This example does not include the details on how to make <code>foo</code> refer to a hardware register of a device connected to the CPU.) Without the <code>volatile</code> keyword, an <a href="Optimizing_compiler" title="Optimizing compiler">optimizing compiler</a> will likely convert the code from the first sample with the read in the loop to the second sample without the read in the loop as part of the common <a href="Loop-invariant_code_motion" title="Loop-invariant code motion">loop-invariant code-motion optimization</a>, and thus the code will likely never notice the change that it is waiting for.
</p><p>To prevent the compiler from doing this optimization, the <code>volatile</code> keyword can be used:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">static</span><span class="w"> </span><span class="k">volatile</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">bar</span><span class="w"> </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">foo</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">255</span><span class="p">)</span>
<span class="w"> </span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>The <code>volatile</code> keyword prevents the compiler from moving the read out of the loop, and thus the code will notice the expected change to the variable <code>foo</code>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Optimization_comparison_in_C">Optimization comparison in C</h3></div>
<p>The following C programs, and accompanying assembler language excerpts, demonstrate how the <code>volatile</code> keyword affects the compiler's output. The compiler in this case was <a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GCC</a>.
</p><p>While observing the assembly code, it is clearly visible that the code generated with <code>volatile</code> objects is more verbose, making it longer so the nature of <code>volatile</code> objects can be fulfilled. The <code>volatile</code> keyword prevents the compiler from performing optimization on code involving volatile objects, thus ensuring that each volatile variable assignment and read has a corresponding memory access. Without the <code>volatile</code> keyword, the compiler knows a variable does not need to be reread from memory at each use, because there should not be any writes to its memory location from any other thread or process.
</p>
<table class="wikitable collapsible collapsed" width="100%">
<tbody><tr>
<th style="text-align: center" colspan="2">Assembly comparison
</th></tr>
<tr>
<th>Without <code>volatile</code> keyword</th>
<th>With <code>volatile</code> keyword
</th></tr>
<tr>
<td>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="cp">#</span><span class="w"> </span><span class="cp">include</span><span class="w"> </span><span class="cpf"><stdio.h></span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="cm">/* These variables will never be created on stack*/</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">100</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="cm">/* "printf" will be called with arguments "%d" and</span>
<span class="cm"> 110 (the compiler computes the sum of a+b),</span>
<span class="cm"> hence no overhead of performing addition at</span>
<span class="cm"> run-time */</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="p">);</span>
<span class="w"> </span><span class="cm">/* This code will be removed via optimization, but</span>
<span class="cm"> the impact of 'c' and 'd' becoming 100 can be</span>
<span class="cm"> seen while calling "printf" */</span>
<span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w"> </span><span class="cm">/* Compiler will generate code where printf is</span>
<span class="cm"> called with arguments "%d" and 200 */</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">d</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</td>
<td>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="cp">#</span><span class="w"> </span><span class="cp">include</span><span class="w"> </span><span class="cpf"><stdio.h></span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">volatile</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">100</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="p">);</span>
<span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">d</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</td></tr>
<tr>
<th><style data-mw-deduplicate="TemplateStyles:r886049734">
/* start https://en.wikipedia.org/ */
.mw-parser-output .monospaced{font-family:monospace,monospace}
/* end https://en.wikipedia.org/ */
</style><span class="monospaced">gcc -S -O3 -masm=intel noVolatileVar.c -o without.s</span>
</th>
<th><span class="monospaced">gcc -S -O3 -masm=intel VolatileVar.c -o with.s</span>
</th></tr>
<tr valign="top">
<td><div class="mw-highlight mw-highlight-lang-asm mw-content-ltr" dir="ltr"><pre><span class="w"> </span><span class="na">.file</span><span class="w"> </span><span class="s">"noVolatileVar.c"</span>
<span class="w"> </span><span class="na">.intel_syntax</span><span class="w"> </span><span class="no">noprefix</span>
<span class="w"> </span><span class="na">.section</span><span class="w"> </span><span class="no">.rodata.str1.1</span><span class="p">,</span><span class="s">"aMS"</span><span class="p">,</span><span class="na">@progbits</span><span class="p">,</span><span class="mi">1</span>
<span class="nl">.LC0:</span>
<span class="w"> </span><span class="na">.string</span><span class="w"> </span><span class="s">"%d"</span>
<span class="w"> </span><span class="na">.section</span><span class="w"> </span><span class="no">.text.startup</span><span class="p">,</span><span class="s">"ax"</span><span class="p">,</span><span class="na">@progbits</span>
<span class="w"> </span><span class="na">.p2align</span><span class="w"> </span><span class="mi">4</span><span class="p">,,</span><span class="mi">15</span>
<span class="w"> </span><span class="na">.globl</span><span class="w"> </span><span class="no">main</span>
<span class="w"> </span><span class="na">.type</span><span class="w"> </span><span class="no">main</span><span class="p">,</span><span class="w"> </span><span class="na">@function</span>
<span class="nl">main:</span>
<span class="nl">.LFB11:</span>
<span class="w"> </span><span class="na">.cfi_startproc</span>
<span class="w"> </span><span class="nf">sub</span><span class="w"> </span><span class="no">rsp</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span>
<span class="w"> </span><span class="na">.cfi_def_cfa_offset</span><span class="w"> </span><span class="mi">16</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="mi">110</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">edi</span><span class="p">,</span><span class="w"> </span><span class="no">OFFSET</span><span class="w"> </span><span class="no">FLAT</span><span class="p">:.</span><span class="no">LC0</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">printf</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="mi">200</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">edi</span><span class="p">,</span><span class="w"> </span><span class="no">OFFSET</span><span class="w"> </span><span class="no">FLAT</span><span class="p">:.</span><span class="no">LC0</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">printf</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">rsp</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span>
<span class="w"> </span><span class="na">.cfi_def_cfa_offset</span><span class="w"> </span><span class="mi">8</span>
<span class="w"> </span><span class="nf">ret</span>
<span class="w"> </span><span class="na">.cfi_endproc</span>
<span class="nl">.LFE11:</span>
<span class="w"> </span><span class="na">.size</span><span class="w"> </span><span class="no">main</span><span class="p">,</span><span class="w"> </span><span class="no">.-main</span>
<span class="w"> </span><span class="na">.ident</span><span class="w"> </span><span class="s">"GCC: (GNU) 4.8.2"</span>
<span class="w"> </span><span class="na">.section</span><span class="w"> </span><span class="no">.note.GNU-stack</span><span class="p">,</span><span class="s">""</span><span class="p">,</span><span class="na">@progbits</span>
</pre></div>
</td>
<td><div class="mw-highlight mw-highlight-lang-asm mw-content-ltr" dir="ltr"><pre><span class="w"> </span><span class="na">.file</span><span class="w"> </span><span class="s">"VolatileVar.c"</span>
<span class="w"> </span><span class="na">.intel_syntax</span><span class="w"> </span><span class="no">noprefix</span>
<span class="w"> </span><span class="na">.section</span><span class="w"> </span><span class="no">.rodata.str1.1</span><span class="p">,</span><span class="s">"aMS"</span><span class="p">,</span><span class="na">@progbits</span><span class="p">,</span><span class="mi">1</span>
<span class="nl">.LC0:</span>
<span class="w"> </span><span class="na">.string</span><span class="w"> </span><span class="s">"%d"</span>
<span class="w"> </span><span class="na">.section</span><span class="w"> </span><span class="no">.text.startup</span><span class="p">,</span><span class="s">"ax"</span><span class="p">,</span><span class="na">@progbits</span>
<span class="w"> </span><span class="na">.p2align</span><span class="w"> </span><span class="mi">4</span><span class="p">,,</span><span class="mi">15</span>
<span class="w"> </span><span class="na">.globl</span><span class="w"> </span><span class="no">main</span>
<span class="w"> </span><span class="na">.type</span><span class="w"> </span><span class="no">main</span><span class="p">,</span><span class="w"> </span><span class="na">@function</span>
<span class="nl">main:</span>
<span class="nl">.LFB11:</span>
<span class="w"> </span><span class="na">.cfi_startproc</span>
<span class="w"> </span><span class="nf">sub</span><span class="w"> </span><span class="no">rsp</span><span class="p">,</span><span class="w"> </span><span class="mi">24</span>
<span class="w"> </span><span class="na">.cfi_def_cfa_offset</span><span class="w"> </span><span class="mi">32</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">edi</span><span class="p">,</span><span class="w"> </span><span class="no">OFFSET</span><span class="w"> </span><span class="no">FLAT</span><span class="p">:.</span><span class="no">LC0</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="p">],</span><span class="w"> </span><span class="mi">10</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">4</span><span class="p">],</span><span class="w"> </span><span class="mi">100</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">8</span><span class="p">],</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">12</span><span class="p">],</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="p">]</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">4</span><span class="p">]</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">printf</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">4</span><span class="p">]</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">edi</span><span class="p">,</span><span class="w"> </span><span class="no">OFFSET</span><span class="w"> </span><span class="no">FLAT</span><span class="p">:.</span><span class="no">LC0</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="p">],</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">4</span><span class="p">]</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">8</span><span class="p">],</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">4</span><span class="p">]</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">12</span><span class="p">],</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">8</span><span class="p">]</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">DWORD</span><span class="w"> </span><span class="no">PTR</span><span class="w"> </span><span class="p">[</span><span class="no">rsp</span><span class="err">+</span><span class="mi">12</span><span class="p">]</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">printf</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">rsp</span><span class="p">,</span><span class="w"> </span><span class="mi">24</span>
<span class="w"> </span><span class="na">.cfi_def_cfa_offset</span><span class="w"> </span><span class="mi">8</span>
<span class="w"> </span><span class="nf">ret</span>
<span class="w"> </span><span class="na">.cfi_endproc</span>
<span class="nl">.LFE11:</span>
<span class="w"> </span><span class="na">.size</span><span class="w"> </span><span class="no">main</span><span class="p">,</span><span class="w"> </span><span class="no">.-main</span>
<span class="w"> </span><span class="na">.ident</span><span class="w"> </span><span class="s">"GCC: (GNU) 4.8.2"</span>
<span class="w"> </span><span class="na">.section</span><span class="w"> </span><span class="no">.note.GNU-stack</span><span class="p">,</span><span class="s">""</span><span class="p">,</span><span class="na">@progbits</span>
</pre></div>
</td></tr></tbody></table>
<div class="mw-heading mw-heading3"><h3 id="Standards_defects">Standards defects</h3></div>
<p>While intended by both C and C++, the current C standard fails to express that the <code>volatile</code> semantics refer to the lvalue, not the referenced object. The respective defect report <i>DR 476</i> (to C11) is still under review with <a href="C17_(C_standard_revision)" title="C17 (C standard revision)">C17</a>.<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Compiler_defects">Compiler defects</h3></div>
<p>Unlike other language features of C and C++, the <code>volatile</code> keyword is not well supported by most C/C++ implementations - even for portable uses according to the C and C++ standards. Most C/C++ implementations are buggy regarding the behavior of the <code>volatile</code> keyword.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> Programmers should take great care whenever using the <code>volatile</code> keyword in C and C++.
</p>
<div class="mw-heading mw-heading2"><h2 id="In_Java">In Java</h2></div>
<p>In all modern versions of the <a href="Java_programming_language" class="mw-redirect" title="Java programming language">Java programming language</a>, the <code>volatile</code> keyword gives the following guarantees:
</p>
<ul><li><code>volatile</code> reads and writes are <a href="Atomic_operation" class="mw-redirect" title="Atomic operation">atomic</a>. In particular, reads and writes to <code>long</code> and <code>double</code> fields will not tear. (The <a href="Atomic_operation" class="mw-redirect" title="Atomic operation">atomic</a> guarantee applies only to the <code>volatile</code> primitive value or the <code>volatile</code> reference value, and <i>not</i> to any Object value.)</li>
<li>There is a single global ordering of all <code>volatile</code> reads and writes. In other words, a <code>volatile</code> read will read the current value (and not a past or future value), and all <code>volatile</code> reads will agree on a single global order of <code>volatile</code> writes.</li>
<li><code>volatile</code> reads and writes have "acquire" and "release" <a href="Memory_barrier" title="Memory barrier">memory barrier</a> semantics (known in the Java standard as <a href="Happened-before" title="Happened-before">happens-before</a>).<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup> In other words, <code>volatile</code> provides guarantees about the relative order of <code>volatile</code> and non-<code>volatile</code> reads and writes. In other words, <code>volatile</code> basically provides the same memory visibility guarantees as a Java <a href="Lock_(computer_science)" title="Lock (computer science)">synchronized block</a> (but without the <a href="Mutual_exclusion" title="Mutual exclusion">mutual exclusion</a> guarantees of a <a href="Lock_(computer_science)" title="Lock (computer science)">synchronized block</a>).</li></ul>
<p>Together, these guarantees make <code>volatile</code> into a useful <a href="Thread_(computing)" title="Thread (computing)">multi-threading</a> construct in <a href="Java_programming_language" class="mw-redirect" title="Java programming language">Java</a>. In particular, the typical <a href="Double-checked_locking" title="Double-checked locking">double-checked locking</a> algorithm with <code>volatile</code> works correctly in <a href="Java_programming_language" class="mw-redirect" title="Java programming language">Java</a>.<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Early_versions_of_Java">Early versions of Java</h3></div>
<p>Before Java version 5, the Java standard did not guarantee the relative ordering of <code>volatile</code> and non-<code>volatile</code> reads and writes. In other words, <code>volatile</code> did not have "acquire" and "release" <a href="Memory_barrier" title="Memory barrier">memory barrier</a> semantics. This greatly limited its use as a <a href="Thread_(computing)" title="Thread (computing)">multi-threading</a> construct. In particular, the typical <a href="Double-checked_locking" title="Double-checked locking">double-checked locking</a> algorithm with <code>volatile</code> did <i>not</i> work correctly.
</p>
<div class="mw-heading mw-heading2"><h2 id="In_C#">In C#</h2></div>
<p>In <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>, <code>volatile</code> ensures that code accessing the field is not subject to some thread-unsafe optimizations that may be performed by the compiler, the CLR, or by hardware. When a field is marked <code>volatile</code>, the compiler is instructed to generate a "memory barrier" or "fence" around it, which prevents instruction reordering or caching tied to the field. When reading a <code>volatile</code> field, the compiler generates an <i>acquire-fence</i>, which prevents other reads and writes to the field from being moved <i>before</i> the fence. When writing to a <code>volatile</code> field, the compiler generates a <i>release-fence</i>; this fence prevents other reads and writes to the field from being moved <i>after</i> the fence.<sup id="cite_ref-Albahari_13-0" class="reference"><a href="#cite_note-Albahari-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p><p>Only the following types can be marked <code>volatile</code>: all reference types, <code>Single</code>, <code>Boolean</code>, <code>Byte</code>, <code>SByte</code>, <code>Int16</code>, <code>UInt16</code>, <code>Int32</code>, <code>UInt32</code>, <code>Char</code>, and all enumerated types with an underlying type of <code>Byte</code>, <code>SByte</code>, <code>Int16</code>, <code>UInt16</code>, <code>Int32</code>, or <code>UInt32</code>.<sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> (This excludes value <a href="Struct" class="mw-redirect" title="Struct">structs</a>, as well as the primitive types <code>Double</code>, <code>Int64</code>, <code>UInt64</code> and <code>Decimal</code>.)
</p><p>Using the <code>volatile</code> keyword does not support fields that are <a href="Evaluation_strategy#Call_by_reference" title="Evaluation strategy">passed by reference</a> or <a href="Closure_(computer_programming)" title="Closure (computer programming)">captured local variables</a>; in these cases, <code>Thread.VolatileRead</code> and <code>Thread.VolatileWrite</code> must be used instead.<sup id="cite_ref-Albahari_13-1" class="reference"><a href="#cite_note-Albahari-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p><p>In effect, these methods disable some optimizations usually performed by the C# compiler, the JIT compiler, or the CPU itself. The guarantees provided by <code>Thread.VolatileRead</code> and <code>Thread.VolatileWrite</code> are a superset of the guarantees provided by the <code>volatile</code> keyword: instead of generating a "half fence" (ie an acquire-fence only prevents instruction reordering and caching that comes before it), <code>VolatileRead</code> and <code>VolatileWrite</code> generate a "full fence" which prevent instruction reordering and caching of that field in both directions.<sup id="cite_ref-Albahari_13-2" class="reference"><a href="#cite_note-Albahari-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup> These methods work as follows:<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup>
</p>
<ul><li>The <code>Thread.VolatileWrite</code> method forces the value in the field to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to <code>VolatileWrite</code> and any later program-order loads and stores must occur after the call.</li>
<li>The <code>Thread.VolatileRead</code> method forces the value in the field to be read from at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to <code>VolatileRead</code> and any later program-order loads and stores must occur after the call.</li></ul>
<p>The <code>Thread.VolatileRead</code> and <code>Thread.VolatileWrite</code> methods generate a full fence by calling the <code>Thread.MemoryBarrier</code> method, which constructs a memory barrier that works in both directions. In addition to the motivations for using a full fence given above, one potential problem with the <code>volatile</code> keyword that is solved by using a full fence generated by <code>Thread.MemoryBarrier</code> is as follows: due to the asymmetric nature of half fences, a <code>volatile</code> field with a write instruction followed by a read instruction may still have the execution order swapped by the compiler. Because full fences are symmetric, this is not a problem when using <code>Thread.MemoryBarrier</code>.<sup id="cite_ref-Albahari_13-3" class="reference"><a href="#cite_note-Albahari-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="In_Fortran">In Fortran</h2></div>
<p><code>VOLATILE</code> is part of the <a href="Fortran#Fortran_2003" title="Fortran">Fortran 2003</a> standard,<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup> although earlier version supported it as an extension. Making all variables <code>volatile</code> in a function is also useful finding <a href="Aliasing_(computing)" title="Aliasing (computing)">aliasing</a> related bugs.
</p>
<div class="mw-highlight mw-highlight-lang-fortran mw-content-ltr" dir="ltr"><pre><span class="kt">integer</span><span class="p">,</span><span class="w"> </span><span class="k">volatile</span><span class="w"> </span><span class="kd">::</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="c">! When not defined volatile the following two lines of code are identical</span>
<span class="k">write</span><span class="p">(</span><span class="o">*</span><span class="p">,</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">i</span><span class="o">**</span><span class="mi">2</span><span class="w"> </span><span class="c">! Loads the variable i once from memory and multiplies that value times itself</span>
<span class="k">write</span><span class="p">(</span><span class="o">*</span><span class="p">,</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">i</span><span class="o">*</span><span class="n">i</span><span class="w"> </span><span class="c">! Loads the variable i twice from memory and multiplies those values</span>
</pre></div>
<p>By always "drilling down" to memory of a VOLATILE, the Fortran compiler is precluded from reordering reads or writes to volatiles. This makes visible to other threads actions done in this thread, and vice versa.<sup id="cite_ref-17" class="reference"><a href="#cite_note-17"><span class="cite-bracket">[</span>17<span class="cite-bracket">]</span></a></sup>
</p><p>Use of VOLATILE reduces and can even prevent optimization.<sup id="cite_ref-18" class="reference"><a href="#cite_note-18"><span class="cite-bracket">[</span>18<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist reflist-columns references-column-width" style="column-width: 30em;">
<ol class="references">
<li id="cite_note-auto-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-auto_1-0">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2016.html">"Publication on C++ standards committee"</a>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://msdn2.microsoft.com/en-us/library/12a04hfd.aspx">"Volatile Keyword In Visual C++"</a>. <i>Microsoft MSDN</i>. 21 September 2021.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.kernel.org/doc/html/latest/process/volatile-considered-harmful.html">"Linux Kernel Documentation – Why the "volatile" type class should not be used"</a>. <i>kernel.org</i>.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite id="CITEREFScott_MeyersAndrei_Alexandrescu2004" class="citation web cs1">Scott Meyers; Andrei Alexandrescu (2004). <a rel="nofollow" class="external text" href="http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf">"C++ and the Perils of Double-Checked Locking"</a> <span class="cs1-format">(PDF)</span>. <i>DDJ</i>.</cite></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite id="CITEREFJeremy_Andrews2007" class="citation web cs1">Jeremy Andrews (2007). <a rel="nofollow" class="external text" href="https://web.archive.org/web/20100620121940/http://kerneltrap.org/Linux/Volatile_Superstition">"Linux: Volatile Superstition"</a>. kerneltrap.org. Archived from <a rel="nofollow" class="external text" href="http://kerneltrap.org/Linux/Volatile_Superstition">the original</a> on 2010-06-20<span class="reference-accessdate">. Retrieved <span class="nowrap">Jan 9,</span> 2011</span>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://msdn.microsoft.com/en-us/library/12a04hfd.aspx">"volatile (C++)"</a>. <i>Microsoft MSDN</i>. 21 September 2021.</cite></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2244.htm"><i>Clarification Request Summary for C11.</i></a> Version 1.13, October 2017.</span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite id="CITEREFEideRegehr2008" class="citation journal cs1">Eide, Eric; Regehr, John (October 2008). <a rel="nofollow" class="external text" href="https://users.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf">"Volatiles Are Miscompiled, and What to Do about It"</a> <span class="cs1-format">(PDF)</span>. <i>Proceedings of the Eighth ACM and IEEE International Conference on Embedded Software (EMSOFT), Atlanta, Georgia, USA</i> – via cs.utah.edu.</cite></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://blog.regehr.org/archives/503">"Volatile Bugs, Three Years Later – Embedded in Academia"</a>. <i>blog.regehr.org</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2024-08-28</span></span>.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text">Section 17.4.4: Synchronization Order
<cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.4">"The Java® Language Specification, Java SE 7 Edition"</a>. <a href="Oracle_Corporation" title="Oracle Corporation">Oracle Corporation</a>. 2013<span class="reference-accessdate">. Retrieved <span class="nowrap">2013-05-12</span></span>.</cite></span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20210509104459/https://dzone.com/articles/java-concurrency-understanding-the-volatile-keyword">"Java Concurrency: Understanding the 'Volatile' Keyword"</a>. dzone.com. 2021-03-08. Archived from <a rel="nofollow" class="external text" href="https://dzone.com/articles/java-concurrency-understanding-the-volatile-keyword">the original</a> on 2021-05-09<span class="reference-accessdate">. Retrieved <span class="nowrap">2021-05-09</span></span>.</cite></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><cite id="CITEREFNeil_Coffey" class="citation web cs1">Neil Coffey. <a rel="nofollow" class="external text" href="http://www.javamex.com/tutorials/double_checked_locking_fixing.shtml">"Double-checked Locking (DCL) and how to fix it"</a>. Javamex<span class="reference-accessdate">. Retrieved <span class="nowrap">2009-09-19</span></span>.</cite></span>
</li>
<li id="cite_note-Albahari-13"><span class="mw-cite-backlink">^ <a href="#cite_ref-Albahari_13-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-Albahari_13-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-Albahari_13-2"><sup><i><b>c</b></i></sup></a> <a href="#cite_ref-Albahari_13-3"><sup><i><b>d</b></i></sup></a></span> <span class="reference-text"><cite id="CITEREFAlbahari" class="citation web cs1">Albahari, Joseph. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20191212032535/http://www.albahari.com/threading/part4.aspx#_Nonblocking_Synchronization">"Part 4: Advanced Threading"</a>. <i>Threading in C#</i>. O'Reilly Media. Archived from the original on 12 December 2019<span class="reference-accessdate">. Retrieved <span class="nowrap">9 December</span> 2019</span>.</cite><span class="cs1-maint citation-comment"><code class="cs1-code">{{cite web}}</code>: CS1 maint: bot: original URL status unknown (link)</span></span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text"><cite id="CITEREFRichter2010" class="citation book cs1">Richter, Jeffrey (February 11, 2010). "Chapter 7: Constants and Fields". <span class="id-lock-limited" title="Free access subject to limited trial, subscription normally required"><a rel="nofollow" class="external text" href="https://archive.org/details/clrviac00rich_000"><i>CLR Via C#</i></a></span>. Microsoft Press. pp. <a rel="nofollow" class="external text" href="https://archive.org/details/clrviac00rich_000/page/n200">183</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-7356-2704-8</bdi>.</cite></span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text"><cite id="CITEREFRichter2010" class="citation book cs1">Richter, Jeffrey (February 11, 2010). "Chapter 28: Primitive Thread Synchronization Constructs". <span class="id-lock-limited" title="Free access subject to limited trial, subscription normally required"><a rel="nofollow" class="external text" href="https://archive.org/details/clrviac00rich_000"><i>CLR Via C#</i></a></span>. Microsoft Press. pp. <a rel="nofollow" class="external text" href="https://archive.org/details/clrviac00rich_000/page/n814">797</a>–803. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-7356-2704-8</bdi>.</cite></span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20180123165050/http://docs.cray.com/books/S-3692-51/html-S-3692-51/zfixedn3c8sk4c.html">"VOLATILE Attribute and Statement"</a>. Cray. Archived from <a rel="nofollow" class="external text" href="http://docs.cray.com/books/S-3692-51/html-S-3692-51/zfixedn3c8sk4c.html">the original</a> on 2018-01-23<span class="reference-accessdate">. Retrieved <span class="nowrap">2016-04-22</span></span>.</cite></span>
</li>
<li id="cite_note-17"><span class="mw-cite-backlink"><b><a href="#cite_ref-17">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://software.intel.com/en-us/forums/intel-moderncode-for-parallel-architectures/topic/279191">"Volatile and shared array in Fortran"</a>. <i>Intel.com</i>.</cite></span>
</li>
<li id="cite_note-18"><span class="mw-cite-backlink"><b><a href="#cite_ref-18">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnbq/index.html">"VOLATILE"</a>. <i>Oracle.com</i>.</cite></span>
</li>
</ol></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://www.adaic.com/standards/05rm/html/RM-C-6.html">Ada Reference Manual C.6: Shared Variable Control</a></li>
<li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20160304053622/https://www.kernel.org/doc/Documentation/volatile-considered-harmful.txt">Linux kernel: volatile-considered-harmful</a></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-05-16" href="https://en.wikipedia.org/wiki/?title=Volatile_(computer_programming)&oldid=1290657603">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>